<template>
  <div class="age-bar" :id="id"></div>
</template>

<script>
  import echarts from 'echarts/lib/echarts'
  import 'echarts/lib/chart/bar'
  import 'echarts/lib/component/tooltip'
  import { mapActions, mapState} from 'vuex'
  export default {
    props: {
      id: {
        type: String,
        required: true
      }
    },
    computed: {
      ...mapState(['ageData']),
    },
    data () {
      return {
        textColor: '#24afcb',
        legendData: '年龄分布',
        barColor: ['#178aa3', '#daca33']
      }
    },
    watch: {
      ageData (val, oldval) {
        this.onReady()
      }
    },
    mounted () {
      this.onReady()
    },
    methods: {
      onReady () {
        const { id, ageData, textColor, barColor } = this
        const options = {
          title: {
            text: '年龄分布',
            textStyle: {
              color: '#ffffff',
              fontWeight: 'normal',
              fontSize: 16
            },
            top: '10%',
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            },
            formatter: function (params) {
              let name = params[0].name
              let name0 = params[0].seriesName
              let value0 = params[0].value
              let dom = `${name}<br/>${name0}:${value0}万人`
              return dom
            }
          },
          grid: {
            left: '3%',
            right: '5%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
            axisLabel: {
              color: textColor,
              formatter: (value, index) => {
                return parseInt(value) + '万'
              }
            },
            axisLine: { show: false }, // 取消边界线
            axisTick: { show: false }, // 取消数字轴上的刻度
            splitLine: { show: false } // 取消背景上的刻度
          },
          yAxis: {
            type: 'category',
            data: ageData.xAxis,
            axisLabel: {
              color: textColor
            },
            axisLine: { show: false },
            axisTick: { show: false }
          },
          series: [
            {
              name: '年龄分布',
              type: 'bar',
              barWidth: '50%',
              barGap: '10%',
              data: ageData.data.map((item) => {
                return (parseInt(item) / 10000).toFixed(4)
              }),
              itemStyle: {
                normal: {
                  color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 1,
                    y2: 1,
                    colorStops: [{
                      offset: 0, color: barColor[0] // 0% 处的颜色
                    }, {
                      offset: 1, color: barColor[1] // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false
                  }
                }
              }
            }
          ]
        }
        const myWave = echarts.init(document.getElementById(id))
        myWave.setOption(options, true)
        window.addEventListener('resize', function () {
          myWave.resize()
        })
      }
    }
  }
</script>

<style lang="css" scoped>
  .age-bar{
    width: 48%;
    height: 100%;
  }
</style>
